/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import model.DoanhThu;
import utility.ConnectDB;
/**
 *
 * @author Dom
 */
public class DoanhThuController {
    
    public static void LayBang(JTable parent, DoanhThu dt) throws SQLException
    {
        
            String sql = "SELECT NgayGio, BienSo, SoTien FROM DoanhThu WHERE '1' = '1' ";
            if(dt.getNam() != -1)
                sql = sql + " AND year(NgayGio) = '"+dt.getNam()+"'";
            if(dt.getThang() != -1)
                sql = sql + " AND (month(NgayGio) = '" + dt.getThang() + "')";
             else
            {
                switch (dt.getQuy())
                {
                    case 1:
                        sql = sql + "AND (month(NgayGio) BETWEEN '01' AND '03')";
                        break;
                    case 2:
                        sql = sql + "AND (month(NgayGio) BETWEEN '04' AND '06')";
                        break;
                    case 3:
                        sql = sql + "AND (month(NgayGio) BETWEEN '07' AND '09')";
                        break;
                    case 4:
                        sql = sql + "AND (month(NgayGio) BETWEEN '10' AND '12')";
                        break;
                }
            }
            
           
           ResultSet table = ConnectDB.getResultSet(sql);
        
        ResultSetMetaData rsmd = table.getMetaData();
        
        int column = rsmd.getColumnCount(); //Số cột
        
        DefaultTableModel model = new DefaultTableModel();  //Model đệm trước khi đẩy vào JTable
        
        //Lấy tiêu đề các cột và gán vào model
        String cname[] = new String[column];
        for (int i=0;i<column;i++)
            cname[i]=rsmd.getColumnName(i+1);
        model.setColumnIdentifiers(cname);
        
        
        //Lấy dữ liệu
        while (table.next())
        {
            String record[] = new String[column];
            for (int i = 0; i<column;i++)
                record[i]=table.getString(i+1);
            
                //if (i==2)
                //System.out.println(table.getObject(i+1));
            
            model.addRow(record);
            
        }
        //Đẩy model lên JTable
        parent.setModel(model);
    }
    
}
